package event;
 
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import control.SpecialCharacter;

import sms.SmsBuffer;
import utility.ConvertToLower;
import utility.Input;
import utility.ReplacesSpecialCharacter;
 
import query.query.Query;
 
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.FileUtility;
 
public class Sendsms implements Event
{
    private boolean Result=false;
    DBManager dbSql;
    
    GetIdName Gid;
    //SearchRecord sr;
    Vector<Object> RECEIVERnumber = new Vector<Object>();
    String msg;
    String tid;
    int index_vt= -1;
    int index_pv=-1;
    int index_ref=-1;
    Hashtable<Object, Object> table =new Hashtable<Object, Object>();
    String Receiver="receiver";
    String QUERY="query";
    String REPLACER="replacer";
    String MESSAGE="message";
    String MOBILENO="mobileno";
    String Process;
    
    public Sendsms()
    {}
    
    public void SetData(String msg, String transaction_type, String tid, String userid)
    {    
    	this.tid =tid;
        RECEIVERnumber.add(userid);
  }
    public void initializationData(DBManager dbSql)
    {
        this.dbSql = dbSql;
        Gid = new GetIdName(dbSql);
    }
    
    public void setObject(String Object)
    {
        try {
            Hashtable<Object, Object> condition =new Hashtable<Object, Object>();
            RecordSearch rs =new RecordSearch(dbSql);
            condition.put("td", "null");
            condition.put("mid", Object);
            rs.setConditionMap(condition);
            String result[] = rs.getArrayFromResultSet();
            
            for(int i=0;i<result.length;i++)
            {
                String splitresult[]=result[i].split("\t");
                String value = splitresult[Column.pv_index-1];
                if(value.equals("0"))
                    value =  splitresult[Column.vt_index-1];
                else
                    value = Gid.getItem(value);
                table.put(Gid.getItem(splitresult[Column.pid_index-1]), value);
            }
            table = ConvertToLower.convertHashKey(table);
            condition.put("pid", Gid.getId(QUERY));
            condition.put("td", "null");
            rs.setConditionMap(condition);
            result = rs.getArrayFromResultSet();
            Vector<Object> QueryVector= new Vector<Object>();
            for(int i=0;i<result.length;i++)
            {
                String splitresult[]=result[i].split("\t");
                String value = splitresult[Column.pv_index-1];
                if(value.equals("0"))
                    value =  splitresult[Column.vt_index-1];
                QueryVector.add(value);
            }
            msg = table.get(MESSAGE).toString();
            RECEIVERnumber =getReceiver(table.get(Receiver).toString());
            String replacerobject =table.get(REPLACER).toString();
            replacerobject = Gid.getId(replacerobject);
            Hashtable<Object, Object> tempcondition=new Hashtable<Object, Object>();
            tempcondition.put("mid", replacerobject);
            tempcondition.put("td", "null");
            rs.setConditionMap(tempcondition);
            result = rs.getArrayFromResultSet();
            table.clear();
            
            for(int i=0;i<result.length;i++)
            {
                String splitresult[] = result[i].split("\t");
                String value = splitresult[Column.pv_index-1];
                if(value.equals("0"))
                    value =  splitresult[Column.vt_index-1];
                else
                    value = Gid.getItem(value);
                table.put(Gid.getItem(splitresult[Column.pid_index-1]).toLowerCase(), value.toLowerCase());
            }
            for(int k=0;k<QueryVector.size();k++)
            {
                String Query = QueryVector.get(k).toString();
                query.query.Query query = new Query();
                query.setDbmanager(dbSql);
                query.setObject(Query);
                if(Process.equals("master_table") || Process.equals("property_details"))
                    query.settypeofreport("autoupdate");
                else
                    query.settypeofreport("Systemgenerate");
                query.setValue(tid);
                query.initializeData();
                query.updateReport();
                String buffer[] =query.getbuffer();            
                if(buffer.length>0)
                    {
                        String splitbuffer[] = buffer[0].split("\t");
                        Vector<Object> temp = new Vector<Object>();
                        for(int i=0;i<splitbuffer.length;i++)
                        {
                            temp.add(splitbuffer[i].toLowerCase());
                        }
                     
                          
                        for(int j=1;j<buffer.length;j++)
                        {
                             Enumeration<Object> enume = table.keys();
                            while(enume.hasMoreElements())
                             { 
                            String property = enume.nextElement().toString().toLowerCase();
                            String replacechar = table.get(property).toString();
                            if(temp.contains(property))
                            {
                                    int index=temp.indexOf(property);
                                    String split[] = buffer[j].split("\t");
                                    if(msg.contains(replacechar))
                                    {
                                        String tempmsg = msg;
                                        int indexofspchar =msg.indexOf(replacechar);
                                        char arr[]=msg.toCharArray();
                                        if(arr[indexofspchar-1]=='+')
                                        {
                                            int tempindex=indexofspchar;
                                            SpecialCharacter sc = new SpecialCharacter();
                                            while(sc.checkSpchar(msg, tempindex))
                                            {
                                                tempindex--;
                                            }   
                                            msg = tempmsg.substring(0,tempindex+1) + " " + split[index] + tempmsg.substring(tempindex+1 ,tempmsg.length());
                                        }   
                                        else
                                        {   
                                        if(j!=buffer.length-1)
                                            msg = tempmsg.substring(0, tempmsg.indexOf(replacechar)) + split[index] + ","+ tempmsg.substring(tempmsg.indexOf(replacechar),tempmsg.length());
                                        else
                                            msg = tempmsg.substring(0, tempmsg.indexOf(replacechar)) + split[index]  + tempmsg.substring( tempmsg.indexOf(replacechar) ,tempmsg.length());
                                        }
                                    }
                                }
                           
                            //    msg =msg.replace(replacechar, "");
                            }
                        }
                    }    
            }
            Enumeration<Object> enume = table.keys();
            while(enume.hasMoreElements())
            {
                String key = enume.nextElement().toString();
                msg =msg.replace(table.get(key).toString(), "");
            }
            msg= msg.replace("+", "");
            table.put("message", msg);
            SmsBuffer smsbuffer =new SmsBuffer();
            for(int i=0;i<RECEIVERnumber.size();i++)
            {
            	smsbuffer.addBufferLog(RECEIVERnumber.get(i).toString(), msg);
            }
            Result =true;
        } catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    
    
    
    public Vector<Object> getReceiver(String reciver)
    {
        Vector<Object> ReceiverList =new Vector<Object>();
         try
        {
            QUERY = Gid.getId(QUERY);
            MOBILENO = Gid.getId(MOBILENO);
            reciver =  Gid.getId(reciver);
            Hashtable<Object, Object> condition = new Hashtable<Object, Object>();
            condition.put("mid", reciver);
            condition.put("td", "null");
            RecordSearch rs =new RecordSearch(dbSql);
            rs.setConditionMap(condition);
            String line[] = rs.getArrayFromResultSet();
            for(int  i=0;i<line.length;i++)
            {
                String splitline[] = line[i].split("\t");
                String value=splitline[Column.pv_index-1];
                if(value.equals("0"))
                    value =splitline[Column.vt_index-1];
                if(splitline[Column.pid_index-1].equals(MOBILENO))
                {
                   if(value.equals("0"))
                       value =splitline[Column.vt_index-1];
                   else
                       value = Gid.getItem(value);
                   ReceiverList.add(value);
                }                
                else if(splitline[Column.pid_index-1].equals(QUERY))
                {    
                    query.query.Query query = new query.query.Query();
                    query.setDbmanager(dbSql);
                    query.setObject(value);
                    if(Process.equals("master_table") || Process.equals("property_details"))
                        query.settypeofreport("autoupdate");
                    else
                        query.settypeofreport("Systemgenerate");
                    query.setValue(tid);
                    query.initializeData();
                    query.updateReport();
                    String buffer[] =query.getbuffer();
                    for(int j=1;j<buffer.length;j++)
                        ReceiverList.add(buffer[j]);
                }
            }
        }
        catch (Exception e)
        {
            
        }
       return ReceiverList;
    }
 
    public boolean getResult()
    {
        //return Result;
        return true;
    }
 
 
    public void SetData(String Product, String type, String masterid,
            String userid, String Process)
    {
        this.tid =masterid;
        this.Process =  Process;
        RECEIVERnumber.add(userid);
    }
    
    
    public static void main(String[] args)
    {
        Sendsms sms =new Sendsms();
        DBManager database =new  DBManager();
        database.getConnect();
        sms.initializationData(database);
        sms.SetData("", "68", "4", "+919922918602","add");
        sms.setObject("10805");
        System.out.println(sms.msg);
    }

	public boolean afterAdd() 
	{
		return Result;
	}

	public boolean afterDel() 
	{
		return Result;
	}

	public boolean afterMod() 
	{
		return Result;
	}

	public boolean afterView() 
	{
		return Result;
	}
}